package cn.imvc.app.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.imvc.app.entities.GoodsClass;
import cn.imvc.app.models.GoodsClassModel;

@Controller
@RequestMapping("/goodsClass")
public class GoodsClassControllerImpl implements GoodsClassController {
	@Autowired
	private GoodsClassModel goodsClassModel;

	/**
	 * 移动端分类首页
	 * 
	 * @return
	 */
	@RequestMapping("/index.html")
	public String index(Model model) {
		/*
		List<GoodsClass> topLevelClassList = goodsClassModel.findTopLevelClass();
		model.addAttribute("topLevelClassList", topLevelClassList);
		if (topLevelClassList != null && topLevelClassList.size() > 0) {
			model.addAttribute("selectedClassId", topLevelClassList.get(0).getId());
		}*/
		model.addAllAttributes(goodsClassModel.findAllWithGroup());
		return "goods-class/list";
	}

	@RequestMapping("/{selectedClassId}.html")
	public String chooseClass(@PathVariable Integer selectedClassId, Model model) {
//		List<GoodsClass> topLevelClassList = goodsClassModel.findTopLevelClass();
//		model.addAttribute("topLevelClassList", topLevelClassList);
		model.addAllAttributes(goodsClassModel.findAllWithGroup());
		model.addAttribute("selectedClassId", selectedClassId);
		return "goods-class/list";
	}

	/**
	 * 获取子类
	 * 
	 * @param parentId
	 * @return
	 */
	@RequestMapping("/getChildClasses.json")
	public @ResponseBody List<GoodsClass> getChildClasses(Integer parentId) {
		return goodsClassModel.findByParentId(parentId);
	}
	@RequestMapping("/grid{parentClassId}.html")
	public String showChildGrid(@PathVariable Integer parentClassId, Model model) {
		model.addAttribute("childrenList",goodsClassModel.findByParentId(parentClassId));
		model.addAttribute("thisClass",goodsClassModel.findById(parentClassId));
		return "goods-class/child-grid";
	}
}
